<template>
  <card class="mb-15">
    <div slot="header">
      <span>费用设置</span>
      <submit-button class="float-right ml-5" ref="Submit" @submit="submit" text="保存"/>
      <el-button class="float-right" @click="getFund" :disabled="!form.username">刷新</el-button>
    </div>
    <el-form :model="form" :rules="rules" ref="Form" label-width="160px" hide-required-asterisk>
      <el-row>
        <el-col :span="8">
          <el-form-item label="网点名称：" prop="userId">
            <search-select-lower v-model="form.userId" @get="get"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="网点资金：" prop="price">{{FORM.price}}</el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="网点子单数：" prop="sonNumber">{{FORM.sonNumber}}</el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="负责人：" prop="name">{{FORM.name}}</el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系方式：" prop="phone">{{FORM.phone}}</el-form-item>
        </el-col>
      </el-row>
      <el-divider/>
      <el-row>
        <el-col :span="8">
          <el-form-item label="快递运费设置："></el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="重量结算：" prop="freight">
            <el-input-number v-model="form.freight" controls-position="right" :precision="2" :min="0"/>
            ₭N/(cm³/{{$volume}})
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider/>
      <el-form-item label="网点结算方式：" prop="websiteClearingWay">
        <el-radio-group v-model="form.websiteClearingWay">
          <el-radio :label="0">寄付</el-radio>
          <el-radio :label="1">到付</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 寄付 -->
      <el-form-item prop="sendWebsiteWay" v-if="form.websiteClearingWay===0">
        <el-radio-group v-model="form.sendWebsiteWay">
          <el-radio :label="0">寄付-运费结算</el-radio>
          <el-radio :label="1">寄付-重量结算</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-row v-if="form.websiteClearingWay===0 && form.sendWebsiteWay===0">
        <el-col :span="8">
          <el-form-item label="寄付-揽收佣金比：" prop="sendCollectFee">
            <el-input-number v-model="form.sendCollectFee" controls-position="right" :precision="2" :min="0" :max="100"/>
            %
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="寄付-中转佣金比：" prop="sendTransferFee">
            <el-input-number v-model="form.sendTransferFee" controls-position="right" :precision="2" :min="0" :max="100"/>
            %
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="寄付-寄付签收佣金比：" prop="sendSignFee">
            <el-input-number v-model="form.sendSignFee" controls-position="right" :precision="2" :min="0" :max="100"/>
            %
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="form.websiteClearingWay===0 && form.sendWebsiteWay===1">
        <el-col>
          <el-form-item label="寄付-重量结算：" prop="sendWebsiteWeightFee">
            <el-input-number v-model="form.sendWebsiteWeightFee" controls-position="right" :precision="2" :min="0"/>
            ₭N/(cm³/{{$volume}})
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 到付 -->
      <el-form-item prop="arriveWebsiteWay" v-if="form.websiteClearingWay===1">
        <el-radio-group v-model="form.arriveWebsiteWay">
          <el-radio :label="0">到付-运费结算</el-radio>
          <el-radio :label="1">到付-重量结算</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-row v-if="form.websiteClearingWay===1 && form.arriveWebsiteWay===0">
        <el-col :span="8">
          <el-form-item label="到付-揽收佣金比：" prop="arriveCollectFee">
            <el-input-number v-model="form.arriveCollectFee" controls-position="right" :precision="2" :min="0" :max="100"/>
            %
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="到付-中转佣金比：" prop="arriveTransferFee">
            <el-input-number v-model="form.arriveTransferFee" controls-position="right" :precision="2" :min="0" :max="100"/>
            %
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="到付-寄付签收佣金比：" prop="arriveSignFee">
            <el-input-number v-model="form.arriveSignFee" controls-position="right" :precision="2" :min="0" :max="100"/>
            %
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="form.websiteClearingWay===1 && form.arriveWebsiteWay===1">
        <el-col>
          <el-form-item label="到付-重量结算：" prop="arriveWebsiteWeightFee">
            <el-input-number v-model="form.arriveWebsiteWeightFee" controls-position="right" :precision="2" :min="0"/>
            ₭N/(cm³/{{$volume}})
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider/>
      <el-form-item label="逾期罚款：" prop="delayedFee">
        <el-input-number v-model="form.delayedFee" controls-position="right" :precision="2" :min="0"/>
      </el-form-item>
    </el-form>
  </card>
</template>

<script>
  import SearchSelectLower from "@/views/components/SearchSelectLower";
  import {getFundApi, updateFundApi} from '@/api/fund'
  import {objectEvaluate, resetForm} from "@/utils/common";

  export default {
    name: "Settings",
    components: {SearchSelectLower},
    data() {
      return {
        FORM: {
          price: null, // 资金
          sonNumber: null, // 子单数
          name: '', // 负责人
          phone: '', // 电话
        },
        form: {
          id: 0, // 网点id
          userId: null, // 网点id
          username: "",  //网点名

          freight: 0, // 运费设置

          websiteClearingWay: 0, // 结算方式(0 寄付 1 到付)

          sendWebsiteWay: 0, // 寄付网点结算方式(0 运费结算 1 重量结算)
          sendCollectFee: 0, // 寄付揽收佣金比
          sendTransferFee: 0, // 寄付中转佣金比
          sendSignFee: 0, // 寄付签收佣金比
          sendWebsiteWeightFee: 0, // 寄付网点重量结算金额

          arriveWebsiteWay: 0, // 到付网点结算方式(0 运费结算 1 重量结算)
          arriveCollectFee: 0, // 到付揽收佣金比
          arriveTransferFee: 0, // 到付中转佣金比
          arriveSignFee: 0, // 到付签收佣金比
          arriveWebsiteWeightFee: 0, // 到付网点重量结算金额

          delayedFee: 0 // 延时操作扣除金额
        },
        rules: {
          userId: {required: true, message: '请选择网点', trigger: 'change'},
          freight: {required: true, message: '请输入重量结算', trigger: 'change'},
          sendCollectFee: {required: true, message: '请输入揽收佣金比', trigger: 'change'},
          sendTransferFee: {required: true, message: '请输入中转佣金比', trigger: 'change'},
          sendSignFee: {required: true, message: '请输入寄付签收佣金比', trigger: 'change'},
          sendWebsiteWeightFee: {required: true, message: '请输入重量结算', trigger: 'change'},
          arriveCollectFee: {required: true, message: '请输入揽收佣金比', trigger: 'change'},
          arriveTransferFee: {required: true, message: '请输入中转佣金比', trigger: 'change'},
          arriveSignFee: {required: true, message: '请输入寄付签收佣金比', trigger: 'change'},
          arriveWebsiteWeightFee: {required: true, message: '请输入重量结算', trigger: 'change'},
          delayedFee: {required: true, message: '请输入逾期罚款', trigger: 'change'},
        }
      }
    },
    methods: {
      get(obj) {
        if (!obj) {
          resetForm(this)
          return
        }
        this.form.username = obj.username;
        this.getFund()
      },
      getFund() {
        let param = {username: this.form.username}
        getFundApi(param).then(result => {
          objectEvaluate(this.form, result.resultParam.fund)
          objectEvaluate(this.FORM, result.resultParam.fund)
        })
      },
      submit() {
        this.$refs['Form'].validate((valid) => {
          if (!valid) return false;
          let data = {...this.form};
          this.$refs.Submit.start();
          updateFundApi(data).then(() => {
            this.$refs.Submit.stop();
          }).catch(() => {
            this.$refs.Submit.stop();
          })
        });
      }
    }
  }
</script>
